﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxDataView;
using DevExpress.Web.ASPxEditors;


namespace Project_Udrinkwater.CustomerLogin
{
    public partial class GoodsOrderLogin : System.Web.UI.Page
    {
        string Goods_ID = "";
        string Number = "";
        string Unit = "";
        string Price = "";
        string Title = "";
        string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

       protected void DataViewGoods_ItemCommand(object source, DataViewItemCommandEventArgs e)
       {
          
            if (e.CommandName =="Cmd")
            {
                for (int i = 0; i < DataViewGoods.VisibleItems.Count; i++)
                {
                   
                    ASPxTextBox TextPrice = (ASPxTextBox)DataViewGoods.FindItemControl("Tb_Price", DataViewGoods.Items[i]);
                    ASPxLabel TextUnit = (ASPxLabel)DataViewGoods.FindItemControl("lb_Unit", DataViewGoods.Items[i]);
                    ASPxLabel lbPrice = (ASPxLabel)DataViewGoods.FindItemControl("lblPrice", DataViewGoods.Items[i]);
                    ASPxLabel lbTitle = (ASPxLabel)DataViewGoods.FindItemControl("lblAddress", DataViewGoods.Items[i]);
                  
                    if (TextPrice.Text !="")
                    {
                        Goods_ID = e.CommandArgument.ToString();
                        Number = TextPrice.Text;
                        Unit = TextUnit.Text;
                        Price = lbPrice.Text;
                        Title = lbTitle.Text;
                        TextPrice.Text = "";

                        AddGoods(Goods_ID, Number,Unit,Price,Title);
                    }
                }
                Response.Write(@"<script language='javascript'>alert('เพิ่มสินค้า "+Goods_ID+" จำนวน "+Number + Unit+"เรียบร้อยแล้ว')</script>");
                
            }
            
        }

        public void AddGoods(string GoodID,string Number,string Unit,string price,string Title)
        {
            string CheckUnit = "";
            if (Unit == "แพ็ค")
            {
                if (Convert.ToInt32(Number) >= 50)
                {
                    CheckUnit = "มีส่วนลด";
                }
                else
                {
                    CheckUnit = "ไม่มีส่วนลด";
                }
            }
            //DataTable dt = new DataTable();
            //dt = GetData(GoodID);

            if (Session["TablePrice"] == null)
            {
                DataTable ListPrice = new DataTable();
                DataRow Row_Price = null;

                //  ListPrice.Columns.Add(new DataColumn("Customer_ID", typeof(string)));
                ListPrice.Columns.Add(new DataColumn("Goods_ID", typeof(string)));
                ListPrice.Columns.Add(new DataColumn("Goos_Name", typeof(string)));
                ListPrice.Columns.Add(new DataColumn("Price", typeof(int)));
                ListPrice.Columns.Add(new DataColumn("PriceNumber", typeof(int)));
                ListPrice.Columns.Add(new DataColumn("Discount", typeof(string)));

               
                Row_Price = ListPrice.NewRow();
                Row_Price["Goods_ID"] = GoodID;
                Row_Price["Goos_Name"] = Title;
                Row_Price["Price"] = Convert.ToInt32(price);
                Row_Price["PriceNumber"] = Convert.ToInt32(Number);
                Row_Price["Discount"] = CheckUnit;
                ListPrice.Rows.Add(Row_Price);

                Session["TablePrice"] = ListPrice;
            }
            else
            {
                DataTable ListPrice = (DataTable)Session["TablePrice"];

                DataRow Row_Price = ListPrice.NewRow();
                Row_Price["Goods_ID"] = GoodID;
                Row_Price["Goos_Name"] = Title;
                Row_Price["Price"] = Convert.ToInt32(price);
                Row_Price["PriceNumber"] = Convert.ToInt32(Number);
                Row_Price["Discount"] = CheckUnit;
                ListPrice.Rows.Add(Row_Price);

                Session["TablePrice"] = ListPrice;
            }
           
        }
        //private DataTable GetData(string GoodID)
        //{
        //    string sqltext = @"SELECT * FROM [TB_ImagePath] WHERE [Image_ID] = '"+GoodID+"' ";
        //    string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        //    SqlConnection sqlconn = new SqlConnection(Connection);
        //    SqlDataAdapter da = new SqlDataAdapter(sqltext, sqlconn);
        //    DataSet ds = new DataSet();
        //    da.Fill(ds);

        //    return ds.Tables[0];

        //}

       
    }
}